package com.aaa.three.service.impl;

import com.aaa.three.mapper.*;
import com.aaa.three.pojo.domain.*;
import com.aaa.three.pojo.query.PersonalQueryy;
import com.aaa.three.result.AjaxResult;
import com.aaa.three.service.UserService;
import com.aaa.three.sysconst.UserConst;
import com.aaa.three.util.SendCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @Author:lyq
 * @Date:2022/2/1415:23
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
     private UserLoginMapper userLoginMapper;
    @Autowired
    private UserEmpMapper userEmpMapper;

    @Autowired
    private HttpSession session;
    @Autowired
    private ChangePasswordMapper changePasswordMapper;
    @Autowired
    HttpServletRequest request;
    @Override
    public AjaxResult login(String username, String password,String telephone) {

        UserLogin login=userLoginMapper.islogin(username,password,telephone);
        if(login == null){
            return  AjaxResult.fail(UserConst.USER_STATE_ZHANGHAOCUOWU);
        }
        if(login.getState().equals(UserConst.USER_STATE_DONGJIE)){
            return  AjaxResult.fail(UserConst.USER_STATE_ZHANGHAODONGJIE);
        }

        if(login.getState().equals(UserConst.USER_STATE_UN_USE)){
            return AjaxResult.fail(UserConst.USER_STATE_ZHANGHAOLAHEI);
        }
        Integer id = login.getEid();
        UserEmp userEmp= userEmpMapper.getEmpByID(id);
        session.setAttribute(UserConst.USER_IN_SESSION,userEmp);


        return AjaxResult.success();


    }

    @Override
    public AjaxResult getInfo() {
        UserEmp emp = (UserEmp) session.getAttribute(UserConst.USER_IN_SESSION);
        return AjaxResult.success(emp);
    }



    @Override
    public AjaxResult changePassword(ChangePassword changePassword) {
        UserEmp emp = (UserEmp) session.getAttribute(UserConst.USER_IN_SESSION);
        Integer id = emp.getId();
        changePassword.setId(id);
        Integer haha=changePasswordMapper.updatePassword(changePassword);
        if(haha!=1){
            return AjaxResult.fail("原密码错误");
        }

    return AjaxResult.success();
    }

    @Override
    public AjaxResult out() {
        request.getSession().removeAttribute("password");
        request.getSession().invalidate();
        return AjaxResult.success();
    }

    @Override
    public AjaxResult sendCode(String telephone) {
        String code=SendCodeUtil.SendCode(telephone);
        System.out.println(code);
        session.setAttribute(telephone,code);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult register(UserRegister userRegister) {
        Object attribute = session.getAttribute(userRegister.getTelephone());
        if(attribute==null){
            AjaxResult.fail("短信失效");
        }
        if(! userRegister.getCode().equals(attribute)){
            return AjaxResult.fail("验证码不匹配");
        }
        UserLogin userLogin= userLoginMapper.repeatPhone(userRegister.getTelephone());
        if(userLogin !=null){
            return AjaxResult.fail("手机号已注册");
        }
        UserLogin login= userLoginMapper.repeatU(userRegister.getUsername());
        if(login!=null){
            return AjaxResult.fail("账号已被注册");
        }

        int  i= userEmpMapper.addInfo(userRegister);
        int a=userLoginMapper.addLogin(userRegister);

        System.out.println(userRegister);



        return AjaxResult.success();
    }

    @Override
    public AjaxResult updatePassword(String telephone, String password) {
       Integer i= userLoginMapper.updatePassword(telephone,password);
       if(i!=1){
           return AjaxResult.fail("手机号未注册");
       }
       return AjaxResult.success();


    }

    @Override
    public AjaxResult nnbl() {
        List<MenWomen> menWomen=userEmpMapper.nnbl();
        return AjaxResult.success(menWomen);
    }


}
